package com.ukao.steward.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseDialogFragment;
import com.ukao.steward.bean.StorageListBean;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.MyDateUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.steward.consts.Constant.ARG_PARAM3;


public class OrderInfoDialogFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_image)
    ImageView closeImage;
    @BindView(R.id.left_order)
    TextView leftOrder;
    @BindView(R.id.orderNo)
    TextView orderNo;
    @BindView(R.id.patState)
    TextView patState;
    @BindView(R.id.statusText)
    TextView statusText;
    @BindView(R.id.createCnt)
    TextView createCnt;
    @BindView(R.id.createTime)
    TextView createTime;
    @BindView(R.id.priority)
    TextView priority;
    @BindView(R.id.product_name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.cardNo)
    TextView cardNo;
    @BindView(R.id.cardName)
    TextView cardName;
    @BindView(R.id.balance)
    TextView balance;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    StorageListBean.OrderInfoBean mOrderInfoBean;
    private boolean isPaid;
    private boolean isEnablePrint;
    private Unbinder unbinder;


    /**
     *
     * @param mOrderInfoBean 订单信息
     * @param isPaid  是否付款
     * @param isEnablePrint  是否打印
     * @return
     */
    public static OrderInfoDialogFragment newInstance(StorageListBean.OrderInfoBean mOrderInfoBean, boolean isPaid, boolean isEnablePrint) {
        OrderInfoDialogFragment fragment = new OrderInfoDialogFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, mOrderInfoBean);
        args.putBoolean(ARG_PARAM2, isPaid);
        args.putBoolean(ARG_PARAM3, isEnablePrint);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mOrderInfoBean = getArguments().getParcelable(ARG_PARAM1);
            isPaid = getArguments().getBoolean(ARG_PARAM2);
            isEnablePrint = getArguments().getBoolean(ARG_PARAM3, false);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_order_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        setIsWithWrap(false);
        setShowTop(true);


        orderNo.setText(mOrderInfoBean.getOrderNo());
        createCnt.setText(mOrderInfoBean.getCreateCnt() + "");
        createTime.setText(MyDateUtils.formatDataTime(mOrderInfoBean.getCreateTime()));
        priority.setText(mOrderInfoBean.getPriority() == 1 ? "加急" : "普通");      /*订单优先级 0.普通 1.加急*/
        statusText.setText(mOrderInfoBean.getStatusText());

        StorageListBean.OrderInfoBean.UserInfoBean userInfo = mOrderInfoBean.getUserInfo();
        name.setText(userInfo.getName());
        phone.setText(CheckUtils.isEmptyString(userInfo.getPhone()));
        cardNo.setText(CheckUtils.isEmptyString(userInfo.getRfidNo()));
        String discount = CheckUtils.isDiscount(userInfo.getCardDiscount(), userInfo.getDiscountType());
        cardName.setText(CheckUtils.isEmptyString(userInfo.getCardName()) + "  " + discount);
        balance.setText(CheckUtils.isEmptyNumber(userInfo.getBalance()) + "");
        setPayStateText(isPaid);
    }


    private void setPayStateText(boolean isPaid) {
        if (isPaid) {/*已付款*/
            patState.setText("已付款");
            patState.setTextColor(resources.getColor(R.color.green));

        } else {
            patState.setText("未付款");
            patState.setTextColor(resources.getColor(R.color.red));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.close_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_image:
                dismiss();
                break;

        }
    }
}
